package com.example.xinli.service.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.xinli.dao.EvalMapper;
import com.example.xinli.dao.InquiryMapper;
import com.example.xinli.dao.InquiryResultMapper;
import com.example.xinli.domain.dto.EvalDto;
import com.example.xinli.domain.dto.InquiryResultDto;
import com.example.xinli.domain.entity.Eval;
import com.example.xinli.domain.entity.Inquiry;
import com.example.xinli.domain.entity.InquiryResult;
import com.example.xinli.domain.vo.ResponseBeanEnum;
import com.example.xinli.service.IInquiryService;
import com.example.xinli.domain.vo.ResponseBean;
import com.example.xinli.utils.TokenUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

@Service
@Slf4j
public class InquiryServiceImpl extends ServiceImpl<InquiryMapper, Inquiry> implements IInquiryService {

    @Autowired
    private InquiryResultMapper inquiryResultMapper;

    @Autowired
    private EvalMapper evalMapper;

    @Override
    public ResponseBean getInquiryByPos(String pos) {
        QueryWrapper<Inquiry> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("position", pos);
        List<Inquiry> inquiry = baseMapper.selectList(queryWrapper);
        return ResponseBean.success(inquiry);
    }

    @Override
    public ResponseBean submitInquiry(String userId,InquiryResultDto[] inquiryResultDto) {
        log.info("submitInquiry");
        int result=1;
        for(InquiryResultDto item : inquiryResultDto){
            InquiryResult inquiryRes=new InquiryResult();
//
//
            inquiryRes.setUserId(userId);
            inquiryRes.setInterId(item.getInterId());
//            inquiryRes.setUserId(item.getUserId());
            inquiryRes.setInquiryId(item.getInquiryId());
            inquiryRes.setScore(item.getScore());
            inquiryRes.setText(item.getText());
            inquiryRes.setFinishedTime(LocalDateTime.now());

            result = inquiryResultMapper.insert(inquiryRes);
//            log.info("result:{}",result);

        }
        if (result == 1) {
            log.info("success");
            return ResponseBean.success();
        } else {
            return ResponseBean.error(ResponseBeanEnum.ERROR);
        }

    }

    @Override
    public ResponseBean submitEval(String userId, EvalDto evalDto){

        Eval eval=new Eval();
        eval.setUserId(userId);
        eval.setInterId(evalDto.getInterId());
        eval.setHarvestScore(evalDto.getHarvestScore());
        eval.setExperienceScore(evalDto.getExperienceScore());
        eval.setHelpfulContent(evalDto.getHelpfulContent());

        evalMapper.insert(eval);

        return ResponseBean.success();

    }

    @Override
    public String getQsByInquiryId(int inquiryId) {
        QueryWrapper<Inquiry> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("inquiry_id", inquiryId);
        List<Inquiry> inquiry = baseMapper.selectList(queryWrapper);
        return inquiry.get(0).getQuestion();

    }

    public Map<Integer, String> getOptionsByInquiryIds(Set<Integer> inquiryIds) {
        if (inquiryIds == null || inquiryIds.isEmpty()) {
            return Collections.emptyMap();
        }

        QueryWrapper<Inquiry> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("inquiry_id", inquiryIds);
        List<Inquiry> inquiries = baseMapper.selectList(queryWrapper);

        // 按 inquiryId 分组，每个 inquiryId 取第一个记录
        Map<Integer, String> inquiryOptions = inquiries.stream()
                .collect(Collectors.groupingBy(
                        Inquiry::getInquiryId,
                        Collectors.collectingAndThen(
                                Collectors.toList(),
                                list -> list.get(0).getOptions()
                        )
                ));

        return inquiryOptions;
    }


    @Override
    public String getOptionsByInquiryId(int inquiryId) {
        QueryWrapper<Inquiry> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("inquiry_id", inquiryId);
        List<Inquiry> inquiry = baseMapper.selectList(queryWrapper);
        return inquiry.get(0).getOptions();

    }

    public Map<Integer, String> getQuestionsByInquiryIds(Set<Integer> inquiryIds) {
        if (inquiryIds == null || inquiryIds.isEmpty()) {
            return Collections.emptyMap();
        }
        
        // 批量查询 Inquiry 信息
        QueryWrapper<Inquiry> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("inquiry_id", inquiryIds);
        List<Inquiry> inquiries = baseMapper.selectList(queryWrapper);

        // 按 inquiryId 分组，每个 inquiryId 取第一个记录的 question
        Map<Integer, String> inquiryQuestions = inquiries.stream()
                .collect(Collectors.groupingBy(
                        Inquiry::getInquiryId,
                        Collectors.collectingAndThen(
                                Collectors.toList(),
                                list -> list.get(0).getQuestion()
                        )
                ));

        return inquiryQuestions;
    }

}

